@extends('admin.common.base-master')

@section('html-head')
    <style type="text/css">
        .divcss5_list_title{background:#eeeeee; border:1px solid #cccccc; padding:1em;}
        .divcss5_list_content{padding:1em;}
        #tinybox{position:absolute; display:none; padding:10px; background:url('{{url('images/preload.gif')}}') no-repeat 50% 50%;  z-index:2000;}
        #tinymask{position:absolute; display:none; top:0; left:0; height:100%; width:100%; background:#000000; z-index:1500;}
        #tinycontent{background:#ffffff; font-size:1.1em;}

        #n{margin:10px auto; width:920px; border:1px solid #CCC;font-size:12px; line-height:30px;}
        #n a{ padding:0 4px; color:#333}
        .sx{padding: 10px}
        .sx-btn{width: 64px;height: 30px;background-color: #4dbcd3;color: #fff;border: #4dbcd3;border-radius: 7%;}
    </style>
    <link href="{{url('css/admin/pagenum.css')}}" rel="stylesheet" />
@endsection

@section('html-body')
    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <a href="#">答案列表</a>
        </div>

        <div class="filter-box clearfix">
            <div class="fr" style="margin-top: 10px;">
                <a class="addBtn" href="{{route('Admin.Home.questionAdd')}}">新增</a>
            </div>
        </div>
        <div class="table-box">
            <table class="table">
                <thead>
                <tr>
                    <th>题目编号</th>
                    <th>答案编号</th>
                    <th>答案内容</th>
                    <th>是否答案</th>
                    <th>更新时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @if(count($answers) > 0)
                    @foreach($answers as $key=>$answer)
                        <tr id="a_{{$answer->id}}">
                            <td>{{$key+1}}</td>
                            <td>{{$answer->select_num}}</td>
                            <td>{{$answer->answer}}</td>
                            <td>@if($answer->is_right == 0)否 @else 是 @endif</td>
                            <td>{{$answer->updated_at}}</td>
                            <td><a class="color2" href="javascript:void(0)" onclick="upPop('{{$answer->select_num}}','{{$answer->answer}}','{{$answer->is_right}}','{{$answer->id}}')">编辑</a> <span>|</span> <a href="javascript:void(0);" class="del" data-id="{{$answer->id}}">删除 </a></td>
                        </tr>
                    @endforeach
                @endif
                </tbody>
                <tfoot class="order-gl">

                </tfoot>
            </table>
        </div>
    </div>
    <div class="upPop-box">
        <div class="backdrop"></div>
        <div id="upPop" class="info-pop tixian">
            <a class="close" href="javascript:void(0);"></a>
            <h3>添加选项</h3>

            <div class="fill-list">
                <span class="fill-tit">选项编号：</span>
                <select id="select_num">
                    <option value="A">A</option>
                    <option value="B">B</option>
                    <option value="C">C</option>
                    <option value="D">D</option>
                    <option value="E">E</option>
                    <option value="F">F</option>
                    <option value="G">G</option>
                </select>
            </div>

            <div class="fill-list">
                <span class="fill-tit">选项内容：</span>
                <input class="fill-ipt" type="text" name="answer" id="answer">
            </div>

            <div class="fill-list">
                <span class="fill-tit">是否答案：</span>
                <input type="radio" name="select_answer" id="is_right" value="1">是
                <input type="radio" name="select_answer" id="no_right" value="0" checked>否
            </div>

            <input type="hidden" name="id" id="answer_id" value="">
            <div class="fill-list">
                <span class="fill-tit">&nbsp;</span>
                <input class="fill-submit" type="button" id="subThisForm" value="提交">
            </div>
        </div>
    </div>
    <script>
        //Api.Question.changeAnswer
        function upPop(selectNum,answer,isRight,id) {
            $("#select_num").val(selectNum);
            $("#answer").val(answer);
            $("#answer_id").val(id);
            $("input:radio[name=select_answer][value="+parseInt(isRight)+"]").attr("checked",true);
            $('.upPop-box').upPop();
        }

        $(function() {
            $(document).on('click', '#subThisForm', function () {
                var id        = $("#answer_id").val();
                var selectNum = $('#select_num option:selected').val();
                var answer    = $('#answer').val();
                var isRight   = $("input[name='select_answer']:checked").val();

                $.ajax({
                    url: '{{route('Api.Question.changeAnswer')}}',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        aid: id,
                        answer:answer,
                        select_num:selectNum,
                        is_right:isRight
                    },
                    success: function (obj) {
                        if (obj.level == "success") {
                            noty({
                                text: obj.message, type: obj.level, timeout: 600,
                                callback: {
                                    afterClose: function () {
                                        window.location.reload();
                                    }
                                }
                            });
                        } else {
                            noty({type: obj.level, text: obj.message, timeout: 600});
                        }
                    },
                    error: function (obj) {
                        console.log(obj);
                    }

                });
            });

            $(document).on("click", ".del", function () {
                var id = $(this).attr("data-id");

                noty({
                    text: '可能影响答题情况，你确定要删除吗?',
                    buttons: [
                        {
                            addClass: 'btn btn-primary', text: '确定', onClick: function ($noty) {
                            $noty.close();
                            $.ajax({
                                url: '{{route('Api.Question.delAnswer')}}',
                                type: 'post',
                                dataType: 'json',
                                data: {aid: id},
                                success: function (obj) {
                                    if (obj.level == "success") {
                                        window.location.reload();
                                    } else {
                                        noty({type: obj.level, text: obj.message, timeout: 600});
                                    }
                                },
                                error: function (obj) {
                                    console.log(obj);
                                }

                            });
                        }
                        },
                        {
                            addClass: 'btn btn-danger', text: '取消', onClick: function ($noty) {
                            $noty.close();
                        }
                        }
                    ]
                });
            });
        });
    </script>
@endsection